//
//  BKDoubleCollectionView.m
//  bestkeep
//
//  Created by utouu_mhm on 16/11/15.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKDoubleCollectionView.h"
#import "BKDoubleCollectionViewCell.h"
#import "BKSCBuyCarTool.h"
#import "BKClassifyNoDataFooter.h"

static NSString *identifier = @"KDoubleCollectionViewCell";

@interface BKDoubleCollectionView ()

@end
@implementation BKDoubleCollectionView

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        
        self.backgroundColor = RGB(242, 242, 242);
        self.dataSource = self;
        self.delegate = self;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        [self registerClass:[BKDoubleCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([BKDoubleCollectionViewCell class])];
        [self registerClass:[BKClassifyNoDataFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NSString stringWithFormat:@"%@footer",NSStringFromClass([BKDoubleCollectionView class])]];
    }
    return self;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.doubleArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BKDoubleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BKDoubleCollectionViewCell class]) forIndexPath:indexPath];
    cell.doubleGoodsModel = self.doubleArray[indexPath.row];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = (ScreenWidth/2.0 - 0.25) * 428/345;
    return CGSizeMake(ScreenWidth/2.0 - 0.25, height);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.25;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
-(BKClassifyNoDataFooter *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
  
    BKClassifyNoDataFooter *Reuseabel = nil;
    if (kind == UICollectionElementKindSectionFooter && self.isNoData) {
        
        Reuseabel = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NSString stringWithFormat:@"%@footer",NSStringFromClass([BKDoubleCollectionView class])] forIndexPath:indexPath];
        return Reuseabel;
    }
    return Reuseabel;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (self.isNoData) {
        
        return CGSizeMake(ScreenWidth, 85);
    }
    
    return CGSizeZero;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.doubleDelegate respondsToSelector:@selector(BKCollectionViewWithIndexPath:)]) {
        
        [self.doubleDelegate BKCollectionViewWithIndexPath:indexPath];
        
    }
}

#pragma mark -滚动的位置的隐藏
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([self.doubleDelegate respondsToSelector:@selector(tableviewCntentOffset:)]) {
        
        [self.doubleDelegate tableviewCntentOffset:scrollView.contentOffset];
    }
}
#pragma mark =======UIScrollViewDelegate

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (velocity.y == 0) {
        
        return;
    }
    
    
    if ([self.doubleDelegate respondsToSelector:@selector(BKCollectionViewWithContentOffset:)]) {
        
        [self.doubleDelegate BKCollectionViewWithContentOffset:velocity];
    }
}


#pragma mark - set方法
-(void)setDoubleArray:(NSArray *)doubleArray
{
    _doubleArray = doubleArray;
    [self reloadData];
}
-(void)setIsNoData:(BOOL)isNoData
{
    _isNoData = isNoData;
    [self reloadData];
}

@end
